package genericdemo;

/**
 * @Author 12629
 * @Description：
 */
class Food {

}
class Fruit extends Food {

}
class Apple extends Fruit {

}
class Banana extends Fruit {

}

class Test <T> { // 设置泛型
    private T val ;

    public T getVal() {
        return val;
    }

    public void setVal(T val) {
        this.val = val;
    }

}

public class TestDemo4 {

    public static void main(String[] args) {
        Integer a = 128;
        Integer b = 128;
        System.out.println(a == b);
    }

    public static void main6(String[] args) {
        int a = 10;
        Integer integer = a;

        int val = integer;//自动拆箱
        System.out.println(val);

        int val2 = integer.intValue();//显示拆箱
        System.out.println(val2);

        double val3 = integer.doubleValue();//显示拆箱
        System.out.println(val3);

    }


    public static void main5(String[] args) {
        int a = 10;
        Integer integer = a;//自动 装箱 -> 底层调用的还是Integer.valueOf
        System.out.println(integer);

      /*  Integer integer2 = new Integer(a);//显示 装箱

        Integer ii = Integer.valueOf(a);//显示 装箱

        System.out.println(integer);
        System.out.println(integer2);
        System.out.println(ii);*/
    }

    public static void main3(String[] args) {
        Test<Fruit> test1 = new Test<>() ;
        test1.setVal(new Fruit());

        fun2(test1);

        Test<Food> test2 = new Test<>() ;
        test2.setVal(new Food());

        fun2(test2);
    }

    //temp 接收Fruit及其子类的一个Message
    public static void fun2(Test<? super Fruit> temp){

        temp.setVal(new Banana());
        temp.setVal(new Apple());

        //Fruit fruit = temp.getVal();
        System.out.println(temp.getVal());//只能直接输出
    }




    public static void main1(String[] args) {
        Test<Apple> test1 = new Test<>() ;
        test1.setVal(new Apple());

        fun(test1);

        Test<Banana> test2 = new Test<>() ;
        test2.setVal(new Banana());

        fun(test2);
    }
    //这里只要是Fruit或者Fruit的子类即可
    public static void fun(Test<? extends Fruit> temp){
        /*
        这里无法确定 temp 引用的是哪个子类对象 所以 无法进行修改
        temp.setVal(new Banana());
        temp.setVal(new Apple());
*/
        //放的都是Fruit或者Fruit的子类,所以 可以直接使用  Fruit 接收
        Fruit banana = temp.getVal();

        System.out.println(temp.getVal());
    }
}
